home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxiscale.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  9.8 KB  |  333 lines

  1. /* Copyright (C) 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxiscale.c,v 1.2 2000/09/19 19:00:38 lpd Exp $ */
  20. /* Interpolated image procedures */
  21. #include "gx.h"
  22. #include "math_.h"
  23. #include "memory_.h"
  24. #include "gpcheck.h"
  25. #include "gserrors.h"
  26. #include "gxfixed.h"
  27. #include "gxfrac.h"
  28. #include "gxarith.h"
  29. #include "gxmatrix.h"
  30. #include "gsccolor.h"
  31. #include "gspaint.h"
  32. #include "gxdevice.h"
  33. #include "gxcmap.h"
  34. #include "gxdcolor.h"
  35. #include "gxistate.h"
  36. #include "gxdevmem.h"
  37. #include "gxcpath.h"
  38. #include "gximage.h"
  39. #include "stream.h"        /* for s_alloc_state */
  40. #include "siinterp.h"        /* for spatial interpolation */
  41. #include "siscale.h"        /* for Mitchell filtering */
  42.  
  43. /*
  44.  * Define whether we are using Mitchell filtering or spatial
  45.  * interpolation to implement Interpolate.  (The latter doesn't work yet.)
  46.  */
  47. #define USE_MITCHELL_FILTER
  48.  
  49. /* ------ Strategy procedure ------ */
  50.  
  51. /* Check the prototype. */
  52. iclass_proc(gs_image_class_0_interpolate);
  53.  
  54. /* If we're interpolating, use special logic. */
  55. private irender_proc(image_render_interpolate);
  56. irender_proc_t
  57. gs_image_class_0_interpolate(gx_image_enum * penum)
  58. {
  59.     const gs_imager_state *pis = penum->pis;
  60.     gs_memory_t *mem = penum->memory;
  61.     stream_image_scale_params_t iss;
  62.     stream_image_scale_state *pss;
  63.     const stream_template *template;
  64.     byte *line;
  65.     const gs_color_space *pcs = penum->pcs;
  66.     const gs_color_space *pccs;
  67.     gs_point dst_xy;
  68.     uint in_size;
  69.  
  70.     if (!penum->interpolate || penum->use_mask_color)
  71.     return 0;
  72.     if (penum->posture != image_portrait || penum->masked || penum->alpha) {
  73.     /* We can't handle these cases yet.  Punt. */
  74.     penum->interpolate = false;
  75.     return 0;
  76.     }
  77.     /*
  78.      * We interpolate using a digital filter, rather than Adobe's
  79.      * spatial interpolation algorithm: this produces very bad-looking
  80.      * results if the input resolution is close to the output resolution,
  81.      * especially if the input has low color resolution, so we resort to
  82.      * some hack tests on the input color resolution and scale to suppress
  83.      * interpolation if we think the result would look especially bad.
  84.      * If we used Adobe's spatial interpolation approach, we wouldn't need
  85.      * to do this, but the spatial interpolation filter doesn't work yet.
  86.      */
  87. #ifdef USE_MITCHELL_FILTERX
  88.     if (penum->bps < 4 || penum->bps * penum->spp < 8 ||
  89.     (fabs(penum->matrix.xx) <= 5 && fabs(penum->matrix.yy <= 5))
  90.     ) {
  91.     penum->interpolate = false;
  92.     return 0;
  93.     }
  94. #endif
  95.     /* Non-ANSI compilers require the following casts: */
  96.     gs_distance_transform((float)penum->rect.w, (float)penum->rect.h,
  97.               &penum->matrix, &dst_xy);
  98.     iss.BitsPerComponentOut = sizeof(frac) * 8;
  99.     iss.MaxValueOut = frac_1;
  100.     iss.WidthOut = (int)ceil(fabs(dst_xy.x));
  101.     iss.HeightOut = (int)ceil(fabs(dst_xy.y));
  102.     iss.WidthIn = penum->rect.w;
  103.     iss.HeightIn = penum->rect.h;
  104.     pccs = cs_concrete_space(pcs, pis);
  105.     iss.Colors = cs_num_components(pccs);
  106.     if (penum->bps <= 8 && penum->device_color) {
  107.     iss.BitsPerComponentIn = 8;
  108.     iss.MaxValueIn = 0xff;
  109.     in_size =
  110.         (penum->matrix.xx < 0 ?
  111.          /* We need a buffer for reversing each scan line. */
  112.          iss.WidthIn * iss.Colors : 0);
  113.     } else {
  114.     iss.BitsPerComponentIn = sizeof(frac) * 8;
  115.     iss.MaxValueIn = frac_1;
  116.     in_size = round_up(iss.WidthIn * iss.Colors * sizeof(frac),
  117.                align_bitmap_mod);
  118.     }
  119.     /* Allocate a buffer for one source/destination line. */
  120.     {
  121.     uint out_size =
  122.         iss.WidthOut * max(iss.Colors * (iss.BitsPerComponentOut / 8),
  123.                    sizeof(gx_color_index));
  124.  
  125.     line = gs_alloc_bytes(mem, in_size + out_size,
  126.                   "image scale src+dst line");
  127.     }
  128. #ifdef USE_MITCHELL_FILTER
  129.     template = &s_IScale_template;
  130. #else
  131.     template = &s_IIEncode_template;
  132. #endif
  133.     pss = (stream_image_scale_state *)
  134.     s_alloc_state(mem, template->stype, "image scale state");
  135.     if (line == 0 || pss == 0 ||
  136.     (pss->params = iss, pss->template = template,
  137.      (*pss->template->init) ((stream_state *) pss) < 0)
  138.     ) {
  139.     gs_free_object(mem, pss, "image scale state");
  140.     gs_free_object(mem, line, "image scale src+dst line");
  141.     /* Try again without interpolation. */
  142.     penum->interpolate = false;
  143.     return 0;
  144.     }
  145.     penum->line = line;
  146.     penum->scaler = pss;
  147.     penum->line_xy = 0;
  148.     {
  149.     gx_dda_fixed x0;
  150.  
  151.     x0 = penum->dda.pixel0.x;
  152.     if (penum->matrix.xx < 0)
  153.         dda_advance(x0, penum->rect.w);
  154.     penum->xyi.x = fixed2int_pixround(dda_current(x0));
  155.     }
  156.     penum->xyi.y = fixed2int_pixround(dda_current(penum->dda.pixel0.y));
  157.     if_debug0('b', "[b]render=interpolate\n");
  158.     return image_render_interpolate;
  159. }
  160.  
  161. /* ------ Rendering for interpolated images ------ */
  162.  
  163. private int
  164. image_render_interpolate(gx_image_enum * penum, const byte * buffer,
  165.              int data_x, uint iw, int h, gx_device * dev)
  166. {
  167.     stream_image_scale_state *pss = penum->scaler;
  168.     const gs_imager_state *pis = penum->pis;
  169.     const gs_color_space *pcs = penum->pcs;
  170.     gs_logical_operation_t lop = penum->log_op;
  171.     int c = pss->params.Colors;
  172.     stream_cursor_read r;
  173.     stream_cursor_write w;
  174.     byte *out = penum->line;
  175.  
  176.     if (h != 0) {
  177.     /* Convert the unpacked data to concrete values in */
  178.     /* the source buffer. */
  179.     int sizeofPixelIn = pss->params.BitsPerComponentIn / 8;
  180.     uint row_size = pss->params.WidthIn * c * sizeofPixelIn;
  181.     const byte *bdata = buffer + data_x * c * sizeofPixelIn;
  182.  
  183.     if (sizeofPixelIn == 1) {
  184.         /* Easy case: 8-bit device color values. */
  185.         if (penum->matrix.xx >= 0) {
  186.         /* Use the input data directly. */
  187.         r.ptr = bdata - 1;
  188.         } else {
  189.         /* Mirror the data in X. */
  190.         const byte *p = bdata + row_size - c;
  191.         byte *q = out;
  192.         int i;
  193.  
  194.         for (i = 0; i < pss->params.WidthIn; p -= c, q += c, ++i)
  195.             memcpy(q, p, c);
  196.         r.ptr = out - 1;
  197.         out = q;
  198.         }
  199.     } else {
  200.         /* Messy case: concretize each sample. */
  201.         int bps = penum->bps;
  202.         int dc = penum->spp;
  203.         const byte *pdata = bdata;
  204.         frac *psrc = (frac *) penum->line;
  205.         gs_client_color cc;
  206.         int i;
  207.  
  208.         r.ptr = (byte *) psrc - 1;
  209.         if_debug0('B', "[B]Concrete row:\n[B]");
  210.         for (i = 0; i < pss->params.WidthIn; i++, psrc += c) {
  211.         int j;
  212.  
  213.         if (bps <= 8)
  214.             for (j = 0; j < dc; ++pdata, ++j) {
  215.             decode_sample(*pdata, cc, j);
  216.         } else        /* bps == 12 */
  217.             for (j = 0; j < dc; pdata += sizeof(frac), ++j) {
  218.             decode_frac(*(const frac *)pdata, cc, j);
  219.             }
  220.         (*pcs->type->concretize_color) (&cc, pcs, psrc, pis);
  221. #ifdef DEBUG
  222.         if (gs_debug_c('B')) {
  223.             int ci;
  224.  
  225.             for (ci = 0; ci < c; ++ci)
  226.             dprintf2("%c%04x", (ci == 0 ? ' ' : ','), psrc[ci]);
  227.         }
  228. #endif
  229.         }
  230.         out += round_up(pss->params.WidthIn * c * sizeof(frac),
  231.                 align_bitmap_mod);
  232.         if_debug0('B', "\n");
  233.     }
  234.     r.limit = r.ptr + row_size;
  235.     } else            /* h == 0 */
  236.     r.ptr = 0, r.limit = 0;
  237.  
  238.     /*
  239.      * Process input and/or collect output.  By construction, the pixels are
  240.      * 1-for-1 with the device, but the Y coordinate might be inverted.
  241.      */
  242.  
  243.     {
  244.     int xo = penum->xyi.x;
  245.     int yo = penum->xyi.y;
  246.     int width = pss->params.WidthOut;
  247.     int sizeofPixelOut = pss->params.BitsPerComponentOut / 8;
  248.     int dy;
  249.     const gs_color_space *pconcs = cs_concrete_space(pcs, pis);
  250.     int bpp = dev->color_info.depth;
  251.     uint raster = bitmap_raster(width * bpp);
  252.  
  253.     if (penum->matrix.yy > 0)
  254.         dy = 1;
  255.     else
  256.         dy = -1, yo--;
  257.     for (;;) {
  258.         int ry = yo + penum->line_xy * dy;
  259.         int x;
  260.         const frac *psrc;
  261.         gx_device_color devc;
  262.         int status, code;
  263.  
  264.         DECLARE_LINE_ACCUM_COPY(out, bpp, xo);
  265.  
  266.         w.limit = out + width *
  267.         max(c * sizeofPixelOut, sizeof(gx_color_index)) - 1;
  268.         w.ptr = w.limit - width * c * sizeofPixelOut;
  269.         psrc = (const frac *)(w.ptr + 1);
  270.         status = (*pss->template->process)
  271.         ((stream_state *) pss, &r, &w, h == 0);
  272.         if (status < 0 && status != EOFC)
  273.         return_error(gs_error_ioerror);
  274.         if (w.ptr == w.limit) {
  275.         int xe = xo + width;
  276.  
  277.         if_debug1('B', "[B]Interpolated row %d:\n[B]",
  278.               penum->line_xy);
  279.         for (x = xo; x < xe;) {
  280. #ifdef DEBUG
  281.             if (gs_debug_c('B')) {
  282.             int ci;
  283.  
  284.             for (ci = 0; ci < c; ++ci)
  285.                 dprintf2("%c%04x", (ci == 0 ? ' ' : ','),
  286.                      psrc[ci]);
  287.             }
  288. #endif
  289.             code = (*pconcs->type->remap_concrete_color)
  290.             (psrc, &devc, pis, dev, gs_color_select_source);
  291.             if (code < 0)
  292.             return code;
  293.             if (color_is_pure(&devc)) {
  294.             /* Just pack colors into a scan line. */
  295.             gx_color_index color = devc.colors.pure;
  296.  
  297.             /* Skip RGB runs quickly. */
  298.             if (c == 3) {
  299.                 do {
  300.                 LINE_ACCUM(color, bpp);
  301.                 x++, psrc += 3;
  302.                 } while (x < xe && psrc[-3] == psrc[0] &&
  303.                      psrc[-2] == psrc[1] &&
  304.                      psrc[-1] == psrc[2]);
  305.             } else {
  306.                 LINE_ACCUM(color, bpp);
  307.                 x++, psrc += c;
  308.             }
  309.             } else {
  310.             int rcode;
  311.  
  312.             LINE_ACCUM_COPY(dev, out, bpp, xo, x, raster, ry);
  313.             rcode = gx_fill_rectangle_device_rop(x, ry,
  314.                              1, 1, &devc, dev, lop);
  315.             if (rcode < 0)
  316.                 return rcode;
  317.             LINE_ACCUM_SKIP(bpp);
  318.             l_xprev = x + 1;
  319.             x++, psrc += c;
  320.             }
  321.         }
  322.         LINE_ACCUM_COPY(dev, out, bpp, xo, x, raster, ry);
  323.         penum->line_xy++;
  324.         if_debug0('B', "\n");
  325.         }
  326.         if ((status == 0 && r.ptr == r.limit) || status == EOFC)
  327.         break;
  328.     }
  329.     }
  330.  
  331.     return (h == 0 ? 0 : 1);
  332. }
  333.